//
//  YKStorageDataViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/11/2.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKStorageDataViewController.h"
#import "YKSelectView.h"
#import "YKClientListHeaderView.h"
#import "YKStorageItem.h"
#import "YKStorageModel.h"
#import "YKSearchViewController.h"
#import "YKPopupView.h"
#import "YKSearchOnlineViewController.h"
#import "KYVideoCheckBoxLabel.h"
#import "YKSaleModel.h"
#import "KYVideoCheckBoxLabel.h"
@interface YKStorageDataViewController ()
@property (nonatomic, strong) YKSelectView *goodsSelectView;
@property(nonatomic, strong) UILabel *bottomLabel;
@property(nonatomic, strong) YKClientListHeaderView *listHeaderView;
@property(nonatomic, strong) KYVideoCheckBoxLabel *checkBox;
@end

@implementation YKStorageDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    YKSaleModel *allModels = [[YKSaleModel alloc]init];
    allModels.GOODS_ID = @"";
    allModels.GOODS_NAME = @"全部";
    NSArray *goods = [@[allModels] arrayByAddingObjectsFromArray:[CommonDataManager sharedInstance].goods];
    self.goodsSelectView = [[YKSelectView alloc]initWithTitle:@"选择品种" row:goods initialText:goods.firstObject];
    self.goodsSelectView.enabled = NO;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onSelectTapped:)];
    self.goodsSelectView.userInteractionEnabled = YES;
    [self.goodsSelectView addGestureRecognizer:tap];
    @weakify(self)
    self.goodsSelectView.selectHandler = ^(id<YKDropdown> value) {
        [self_weak_ startLoadData];
    };
    [self.view addSubview:self.goodsSelectView];
    [self.goodsSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.view);
    }];

    self.checkBox = [[KYVideoCheckBoxLabel alloc]init];
    self.checkBox.text = @"云康品种";
    self.checkBox.checkBoxSize = CGSizeMake(22, 22);
    [self.view addSubview:self.checkBox];
    self.checkBox = self.checkBox;
    [self.checkBox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading).offset(8);
        make.top.equalTo(self.goodsSelectView.mas_bottom);
        make.trailing.equalTo(self.view.mas_trailing);
        make.height.mas_equalTo(40);
    }];
    self.checkBox.checkBoxStateChanged = ^(BOOL on) {
        [self startLoadData];
    };

    self.listHeaderView = [[YKClientListHeaderView alloc] initWithTitles:@[@"品种",@"库存数量"] weights:@[@2,@1]];
    self.listHeaderView.textFont = [UIFont systemFontOfSize:16];
    [self.view addSubview:self.listHeaderView];
    [self.listHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.view.mas_top);
    }];
    self.listHeaderView.backgroundColor = [UIColor tf_colorWithHexString:@"#1EADFC"];
    
    self.listHeaderView.textFont = [UIFont systemFontOfSize:16];


    self.bottomLabel = [[UILabel alloc] init];
    self.bottomLabel.text = @"";
    self.bottomLabel.font = [UIFont boldSystemFontOfSize:16];
    self.bottomLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.bottomLabel];
    [self.bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view);
        make.height.mas_equalTo(30);
    }];

    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.listHeaderView.mas_bottom);
        make.bottom.equalTo(self.bottomLabel.mas_top);
    }];

    self.dataSource.totalPageKey = @"page.totalpage";
    self.dataSource.currentPageKey = @"curpage";
    [self startLoadData];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view bringSubviewToFront:self.tableView];
    self.tableView.tableFooterView = [UIView new];
    UIBarButtonItem *filterItem = [[UIBarButtonItem alloc] initWithTitle:@"筛选" style:UIBarButtonItemStylePlain target:self action:@selector(onFilterItemTapped:)];
    self.navigationItem.rightBarButtonItem = filterItem;
}

- (void)onFilterItemTapped:(UIBarButtonItem *)item {
    if (item.tag == 0) {
        [UIView animateWithDuration:.3f animations:^{
            [self.listHeaderView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.leading.trailing.equalTo(self.view);
                make.top.equalTo(self.checkBox.mas_bottom);
            }];
            [self.view layoutIfNeeded];
        }];

        item.tag = 1;
    } else{
        item.tag = 0;
        [UIView animateWithDuration:.3f animations:^{
            [self.listHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.trailing.equalTo(self.view);
                make.top.equalTo(self.view.mas_top);
            }];
            [self.view layoutIfNeeded];
        }];
    }

}

- (void)loadData {
    [self startLoadData];
}

- (void)onSelectTapped:(id)sender {
    @weakify(self)
    YKSearchOnlineViewController *vc = [YKSearchOnlineViewController new];
    __weak YKSearchOnlineViewController *wvc = vc;
    vc.selectDrugComplete = ^(id<YKDropdown> model) {
        self_weak_.goodsSelectView.selectedRow = model;
        [YKPopupView dismissViewController:wvc];
    };
    vc.dataSource = self.goodsSelectView.data;
    
    [YKPopupView showPopupViewWithViewController:vc height:self.view.tf_height - 150];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.dataSource.manager.sections.count > 0) {
        [self startLoadDataSilently];
    }
}

- (NSDictionary *)pageViewControllerParameters {
    return @{
            kTFPageViewControllerItemClass: [YKStorageItem class],
            kTFPageViewControllerModelClass:[YKStorageModel class],
            kTFPageViewControllerRequestURL:@"product/searchStockList.htm",
            kTFPageViewControllerDataKeyPath:@"list",
            kTFPageViewControllerRequestParameter:@{@"pid":self.goodsSelectView.selectedRow.id_p?:@"",@"isPlatformProduct":@(self.checkBox.on?1:0)}
    };
}

- (void)didFinishLoad:(TFDataLoadPolicy)loadPolicy object:(id)object sections:(NSArray<TFTableViewSection *> *)sections error:(NSError *)error {
    [super didFinishLoad:loadPolicy object:object sections:sections error:error];
    self.bottomLabel.text = [NSString stringWithFormat:@"共%d行",sections.firstObject.items.count];
}

- (void)customizeItem:(TFTableViewItem *)item {
    @weakify(self)
    item.selectionHandler = ^(__kindof YKStorageItem *item, NSIndexPath *indexPath) {

    };
}

@end
